//
//  FeedbacKView.m
//  KYProject
//
//  Created by Mxxx on 2016/12/22.
//  Copyright © 2016年 Roman. All rights reserved.
//

#import "FeedbacKView.h"
#import "ProgressManager.h"
#import "DefineConstant.h"
#define MAX_NUM 200

@interface FeedbacKView()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>

@property (nonatomic,weak)UILabel *lbl;
@property (nonatomic,weak)UITextView *textView;

@end
@implementation FeedbacKView
static NSString *cellId = @"cellId";
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    //testView
    UITextView *testView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
    testView.delegate = self;
    [self addSubview:testView];
    self.textView = testView;
    
   
    
    //lbl
    UILabel *lbl = [[UILabel alloc]init];
    lbl.text = [NSString stringWithFormat:@"你还可以输入%d个子",MAX_NUM];
    lbl.font = MYFONT(12);
    lbl.textAlignment = NSTextAlignmentCenter;
    [self addSubview:lbl];
    self.lbl = lbl;
    //btn
    UIButton *btn = [[UIButton alloc]init];
    [btn setImage:[UIImage imageNamed:@"dijiaofankui"] forState:UIControlStateNormal];
    [self addSubview:btn];
    
    //点击
    [btn addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
    
    //tableView
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
    
    [self addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 40;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
    
    //约束
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self);
        make.top.equalTo(testView.mas_bottom);
        make.width.equalTo(@69);
        make.height.equalTo(@31);
    }];
    
    [lbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self);
        make.top.equalTo(testView.mas_bottom);
        make.trailing.equalTo(btn.mas_leading);
        make.centerY.equalTo(btn);
    }];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self);
        make.top.equalTo (btn.mas_bottom);
    }];
}

//tableView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.row == 0) {
        cell.imageView.image = [UIImage imageNamed:@"dianhua"];
        cell.textLabel.text = @"客服电话400-691-233";
        cell.textLabel.font = MYFONT(15);
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.textLabel.backgroundColor = [UIColor clearColor];
    }
    else
    {
        cell.imageView.image = [UIImage imageNamed:@"qq"];
        cell.textLabel.text = @"用户交流群1111111";
        cell.textLabel.font = MYFONT(15);
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.textLabel.backgroundColor = [UIColor clearColor];
    }
    //设置分割线
    UIImageView *separatView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"table_item_bk_with_top_gray_corner"]];
    [cell.contentView addSubview:separatView];
    [separatView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(cell.contentView);
        make.height.mas_equalTo(5);
    }];
    return cell;
}

#pragma mark - 点击cell打电话
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setSelected:NO animated:YES];
    if (indexPath.row == 0) {
        //打电话
        NSURL *url = [NSURL URLWithString:@"tel://10010"];
        [[UIApplication sharedApplication]openURL:url];
    }
}

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"欢迎联系我们";
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

//textView的代理方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(nonnull NSString *)text
{
    NSString *str = [textView.text stringByReplacingCharactersInRange:range withString:text];
    NSInteger num = MAX_NUM - str.length;
    if (num >= 0) {
        return YES;
    }
    else
    {
        NSInteger len = text.length + num;
        //防止当text.length + caninputlen < 0 时,使得rg.length为一个非法最大正数出错
        NSRange rg = {0,MAX(len,0)};
        if (rg.length > 0) {
            NSString *s = [text substringWithRange:rg];
            [textView setText:[textView.text stringByReplacingCharactersInRange:range withString:s]];
        }
        return NO;
    }
}

- (void)textViewDidChange:(UITextView *)textView
{
    NSString *nsTextContent = textView.text;
    NSInteger existTextNum = nsTextContent.length;
    
    if (existTextNum > MAX_NUM) {
        //截取到最大位置的字符
        NSString *s = [nsTextContent substringToIndex:MAX_NUM];
        [textView setText:s];
        existTextNum = MAX_NUM;
    }
    self.lbl.text = [NSString stringWithFormat:@"你还可以输入%1d数字",MAX(0,MAX_NUM - existTextNum)];
}

#pragma mark - 点击了反馈按钮
- (void)clickBtn:(UIButton *)sender
{
    self.textView.text = @"";
    
    //用工具类框架的方法
    [ProgressManager alertSuccessStatus:@"提交成功"];
    self.lbl.text = [NSString stringWithFormat:@"你还可以输入%d个字",MAX_NUM];
    
    [self.textView endEditing:YES];

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
